<?php

namespace App\Http\Controllers\Api\Alipay;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Service\Alipay\AlipayRequestService;

class AlipayRequestController extends Controller
{

    private $client;

    public function __construct( )
    {
        $this->client = new AlipayRequestService();
    }


    /**
     * 会员卡模板创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardTemplateCreate(Request $request)
    {

        $biz_content = $request->input('biz_content');
        if ( empty($biz_content) ) {
            return response()->json(['code' => 100001, 'message' => '请求参数不能为空' ]);
        }

        $this->client->method = 'alipay.marketing.card.template.create';
        $this->client->biz_content = $biz_content;
        $this->client->response_key = 'alipay_marketing_card_template_create_response';
        return response()->json( $this->client->query() );
        
    }


    /**
     * 会员卡模板修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardTemplateModify(Request $request)
    {

        $biz_content = $request->input('biz_content');
        if ( empty($biz_content) ) {
            return response()->json(['code' => 100001, 'message' => '请求参数不能为空' ]);
        }

        $this->client->method = 'alipay.marketing.card.template.modify';
        $this->client->biz_content = $biz_content;
        $this->client->response_key = 'alipay_marketing_card_template_modify_response';
        return response()->json( $this->client->query() );
    }


    /**
     * 会员卡开卡表单模板配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardFormtemplateSet(Request $request)
    {

        $biz_content = $request->input('biz_content');
        if ( empty($biz_content) ) {
            return response()->json(['code' => 100001, 'message' => '请求参数不能为空' ]);
        }

        $this->client->method = 'alipay.marketing.card.formtemplate.set';
        $this->client->biz_content = $biz_content;
        $this->client->response_key = 'alipay_marketing_card_formtemplate_set_response';
        return response()->json( $this->client->query() );
    }


    /**
     * 获取会员卡领卡投放链接
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardActivateurlApply(Request $request)
    {

        $biz_content = $request->input('biz_content');
        if ( empty($biz_content) ) {
            return response()->json(['code' => 100001, 'message' => '请求参数不能为空' ]);
        }

        $this->client->method = 'alipay.marketing.card.activateurl.apply';
        $this->client->biz_content = $biz_content;
        $this->client->response_key = 'alipay_marketing_card_activateurl_apply_response';
        return response()->json( $this->client->query() );
    }


    /**
     * 获取会员卡领卡投放链接
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cardActivateformQuery(Request $request)
    {

        $biz_content = $request->input('biz_content');
        if ( empty($biz_content) ) {
            return response()->json(['code' => 100001, 'message' => '请求参数不能为空' ]);
        }

        $this->client->method = 'alipay.marketing.card.activateform.query';
        $this->client->biz_content = $biz_content;
        $this->client->response_key = 'alipay_marketing_card_activateform_query_response';
        return response()->json( $this->client->query() );
    }


    /**
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function materialImageUpload(Request $request)
    {

        $image_type = $request->input('image_type');
        $image_name = $request->input('image_name', '1.jpg');
        $image_content = $request->input('image_content');

        if (empty($image_type)) {
            return response()->json(['code' => 100001, 'message' => '图片/视频格式不能为空' ]);
        }
        if (empty($image_name)) {
            return response()->json(['code' => 100002, 'message' => '图片/视频名称不能为空' ]);
        }
        if (empty($image_content)) {
            return response()->json(['code' => 100003, 'message' => '图片/视频地址不能为空' ]);
        }

        $this->client->method = 'alipay.offline.material.image.upload';
        $this->client->biz_content = [
            'image_type' => $image_type,
            'image_name' => $image_name,
            'image_content' => '@' . $image_content,
        ];

        $this->client->response_key = 'alipay_offline_material_image_upload_response';

        return response()->json( $this->client->query() );

    }


    /**
     * 查询商户的门店编号列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function marketShopBatchquery(Request $request)
    {

        $page_no = $request->input('page_no');
        if ( empty($page_no) ) {
            return response()->json(['code' => 100001, 'message' => '请求页码不能为空' ]);
        }

        $this->client->method = 'alipay.offline.market.shop.batchquery';
        $this->client->biz_content = $page_no;

        $this->client->response_key = 'alipay_offline_market_shop_batchquery_response';
        return response()->json( $this->client->query() );

    }

    /**
     * 查询单个门店信息接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function marketShopQueryDetail(Request $request)
    {

        $shop_id = $request->input('shop_id');
        if ( empty($shop_id) ) {
            return response()->json(['code' => 100001, 'message' => '请求支付宝门店ID不能为空' ]);
        }

        $this->client->method = 'alipay.offline.market.shop.querydetail';
        $this->client->biz_content = $shop_id;

        $this->client->response_key = 'alipay_offline_market_shop_querydetail_response';
        return response()->json( $this->client->query() );

    }



}